#    This is part of WeatherFront.
#
#    Copyright (C) 2010 Patrice Köttig
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; version 2 of the License.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

import Foundation
import time

from WeatherTranslate import tr

languageCode=Foundation.NSUserDefaults.standardUserDefaults().objectForKey_('AppleLanguages')[0]
#languageCode='de'

def parseDate(str, id):
	strCpy = str
	if languageCode == 'en':
		return str
	try:
		if id == "cc":
			pos = str.rfind("/")
			if pos != -1:
				pos = pos + 1
				str = str[:pos] + "20" + str[pos:]
				hourOffset = 0
				if str.rfind("AM") != -1:
					t1 = time.strptime(str, "%m/%d/%Y %H:%M AM")
					if t1.tm_hour == 12:
						hourOffset = -12
				elif str.rfind("PM") != -1:
					t1 = time.strptime(str, "%m/%d/%Y %H:%M PM")
					if t1.tm_hour != 12:
						hourOffset = 12
				elif str.rfind("Midnight") != -1:
					t1 = time.strptime(str, "%m/%d/%Y %H:%M Midnight")
					hourOffset = -12
				elif str.rfind("Noon") != -1:
					t1 = time.strptime(str, "%m/%d/%Y %H:%M Noon")
				else:
					return strCpy
				t1 = time.struct_time((t1.tm_year, t1.tm_mon, t1.tm_mday, t1.tm_hour + hourOffset, t1.tm_min, 0, 0, 0, 0))
				str = time.strftime(tr("%Y-%m-%dT%H:%M"), t1)
			else:
				str = strCpy
		else:
			list = str.split(" ")
			map = {'Jan' : 1, 'Feb' : 2, 'Mar' : 3, 'Apr' : 4, 'May' : 5, 'Jun' : 6, 'Jul' : 7, 'Aug' : 8, 'Sep' : 9, 'Oct' : 10, 'Nov' : 11, 'Dec' : 12 }
			t1 = time.localtime()
			t1 = time.struct_time((t1.tm_year, map[list[0]], int(list[1]), 0, 0, 0, 0, 0, 0))
			str = time.strftime(tr("%B %d"), t1)
			month = time.strftime("%B", t1)
			str = str.replace(month, tr(month))
	except:
		str = strCpy
	return str
	
def parseDirection(str):
	strCpy = str
	#if languageCode == 'en':
	#	return str
	try:
		str = str.replace("N", tr("N") + "-")
		str = str.replace("S", tr("S") + "-")
		str = str.replace("E", tr("E") + "-")
		str = str.replace("W", tr("W") + "-")
		str = str.replace("VAR", tr("variable") + "-")
		str = str[:-1]
	except:
		str = strCpy
	return str
	
def parseLocation(str):
	strCpy = str
	if languageCode == 'en':
		return str
	try:
		list = str.split(", ")
		str = str.replace(list[1], tr(list[1]))
	except:
		str = strCpy
	return str
